package com.example.smartcity_poverty.ui.AllService.NumberBook.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.model.NumberBook.NumberBookUserAllModel
import com.example.smartcity_poverty.ui.AllService.NumberBook.NumberBookViewModel
import kotlinx.android.synthetic.main.activity_show_u_iser_all.*
import kotlinx.android.synthetic.main.re_number_book_user_list.*


/**
 * 图书馆全部评论展示
 */
class ShowUserAllActivity : AppCompatActivity() {

    private val vm by lazy {
        ViewModelProviders.of(this)[NumberBookViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_show_u_iser_all)
        title = "图书馆全部评论"

        val id = intent.getStringExtra("id")?.toInt()

        // 填充数据
        id?.let {
            setIdShow(id)
        }

    }

    /**
     * 填充数据
     */
    private fun setIdShow(id: Int) {
        vm.setBookId(id)
        vm.mBookLiveData.observe(this, Observer { resources ->
            resources.getOrNull()?.apply {
                vm.mBookList.clear()
                vm.mBookList.addAll(this.data!!)

                // 填充数据
                showUserListRv.apply {
                    this.layoutManager = LinearLayoutManager(context)
                    this.adapter = RvAdapter(vm.mBookList, R.layout.re_number_book_user_list)
                }
            }
        })
    }

    /**
     * 列表适配器
     */
    inner class RvAdapter(
       val list: MutableList<NumberBookUserAllModel.DataBean>,
       val layoutId: Int
    ) : BaseRecyclerAdapter<NumberBookUserAllModel.DataBean>(list, layoutId){
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: NumberBookUserAllModel.DataBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val v = holder.viewFind
            val con = v.view?.context

            // 填充数据
            v.sUserName.text = d.userName
            v.sUserContent.text = d.content
        }

    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}